//
//  FQDEditLongSettingViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/11/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDEditLongSettingViewController.h"
#import "FQDNavigationView.h"
#import "MBProgressHUD+FQDExtension.h"
#import "FQDAccountManager.h"
#import "FQDRestModel.h"

#import "Masonry.h"

static NSInteger const kDefaultMaxWordCount = 34;

@interface FQDEditLongSettingViewController () <UITextViewDelegate>

@property (nonatomic, strong) UIView *shadowBgView;
@property (nonatomic, strong) UIVisualEffectView *blurBgView;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *wordCountLabel;

@end

@implementation FQDEditLongSettingViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _maxWordCount = kDefaultMaxWordCount;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.hideKeyboardOnTap = YES;
    self.navView.titleLabel.text = self.title;
    [self.navView.rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [self.navView.rightButton addTarget:self action:@selector(saveSetting:) forControlEvents:UIControlEventTouchUpInside];
    
    self.shadowBgView = [[UIView alloc] init];
    self.shadowBgView.backgroundColor = [UIColor clearColor];
    self.shadowBgView.layer.borderWidth = 0.5f;
    self.shadowBgView.layer.borderColor = [RGB(178, 178, 178) CGColor];
    [self.view addSubview:self.shadowBgView];
    
    WeakSelf;
    [self.shadowBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(100);
        make.left.equalTo(weakSelf.view);
        make.right.equalTo(weakSelf.view);
        make.height.equalTo(@120);
    }];
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.blurBgView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    self.blurBgView.frame = self.shadowBgView.bounds;
    self.blurBgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.shadowBgView addSubview:self.blurBgView];
    
    self.textView = [[UITextView alloc] init];
    self.textView.font = FQDRegularFontWithSize(17.0f);
    self.textView.textColor = RGB(170, 170, 170);
    self.textView.tintColor = FQDHighlightColor;
    self.textView.backgroundColor = [UIColor clearColor];
    self.textView.text = self.currentSettingValue;
    self.textView.delegate = self;
    [self.textView becomeFirstResponder];
    [self.view addSubview:self.textView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.shadowBgView).offset(10);
        make.left.equalTo(weakSelf.shadowBgView).offset(10);
        make.right.equalTo(weakSelf.shadowBgView).offset(-10);
        make.bottom.equalTo(weakSelf.shadowBgView).offset(-10);
    }];
    
    self.wordCountLabel = [[UILabel alloc] init];
    self.wordCountLabel.font = FQDRegularFontWithSize(17.0f);
    [self.view addSubview:self.wordCountLabel];
    
    [self.wordCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.blurBgView.mas_bottom).offset(10);
        make.right.equalTo(weakSelf.blurBgView.mas_right).offset(-20);
    }];
    
    [self updateWordCountLabel];
}

- (void)updateWordCountLabel
{
    NSString *inputString = self.textView.text;
    NSInteger remainWordCount = self.maxWordCount - [inputString length] ;
    self.wordCountLabel.text = [@(remainWordCount) stringValue];
    if (remainWordCount > 0) {
        self.wordCountLabel.textColor = RGB(128, 128, 128);
    } else {
        self.wordCountLabel.textColor = [UIColor redColor];
    }
}

#pragma mark - Event response
- (void)saveSetting:(id)sender
{
    NSString *desiredValue = self.textView.text;
    NSString *trimmedString = [desiredValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([trimmedString length] <= 0) {
        [MBProgressHUD showMessage:@"修改的信息不能为空"];
        return;
    }

    if ([desiredValue length] > self.maxWordCount) {
        [MBProgressHUD showMessage:[NSString stringWithFormat:@"不能超过%@个字", @(self.maxWordCount)]];
        return;
    }



    [[QDailyAPI sharedAPI] updatePersonalInfo:self.settingType infoValue:desiredValue
     completion:^(id responseObject, NSError *error) {
        NSNumber *status = [responseObject valueForKeyPath:@"meta.status"];
        if ([status integerValue] == 200) {
            id response = responseObject[@"response"];
            FQDUser *user = [FQDUser yy_modelWithJSON:response];
            [[FQDAccountManager sharedManager] changeCurrentUser:user];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

#pragma mark - UITextViewDeleagte
- (void)textViewDidChange:(UITextView *)textView
{
    [self updateWordCountLabel];
}

@end
